package com.inews.business.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inews.base.mapper.JsonMapper;
import com.inews.base.utils.InewsUtils;
import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.SysRoleService;
import com.inews.business.vo.RoleResourceTreeVo;
import com.inews.business.vo.SysRoleVo;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
	
	@Autowired
	private SysRoleService roleService;
	
	/**
	 * 分页查询系统用户信息.
	 *
	 * @param request the request
	 * @param page the page
	 * @return the object
	 */
	@RequestMapping("findSysRolePage")
	@ResponseBody
	public Object findSysRolePage(ServletRequest request, @RequestParam(value = "page", defaultValue = "1") int pageNumber, String sort){
		Pageable page = Servlets.buildPageRequest(pageNumber, sort);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		ExtData<SysRoleVo> extData = roleService.findSysRolePage(searchParams, page);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 查询所有角色.
	 *
	 * @return the object
	 */
	@RequestMapping("findSysRoleAll")
	@ResponseBody
	public Object findSysRoleAll(){
		ExtData<SysRoleVo> extData = roleService.findSysRoleAll();
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 保存用户信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("saveSysRole")
	@ResponseBody
	public Object saveSysRole(SysRoleVo vo){
		ExtData<SysRoleVo> extData = roleService.saveSysRole(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 更新用户信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("updateSysRole")
	@ResponseBody
	public Object updateSysRole(SysRoleVo vo){
		ExtData<SysRoleVo> extData = roleService.updateSysRole(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 删除用户信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("deleteSysRole")
	@ResponseBody
	public Object deleteSysRole(SysRoleVo vo){
		ExtData<SysRoleVo> extData = roleService.deleteSysRole(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 获取角色对应权限.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("findRoleResource")
	@ResponseBody
	public Object findRoleResource(SysRoleVo vo){
		if(InewsUtils.isEmpty(vo) || InewsUtils.isEmpty(vo.getRoleId())){
			return null;
		}
		ExtData<RoleResourceTreeVo> extData = roleService.findRoleResource(vo);
		String json = JsonMapper.nonEmptyMapper().toJson(extData.getRoot().get(0).getChildren());
		return json;
	}
	
	
	
	/**
	 * 分配权限.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("chooseResource")
	@ResponseBody
	public Object chooseResource(SysRoleVo vo, String resourceTree){
		List<RoleResourceTreeVo> roleResourceVos = JsonMapper.nonDefaultMapper().fromJson(resourceTree, JsonMapper.nonDefaultMapper().createCollectionType(List.class, RoleResourceTreeVo.class));
		ExtData<SysRoleVo> extData = roleService.chooseResource(vo, roleResourceVos, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	

}
